8985. Delete a letter

 

Delete all lowercase Latin letters ‘a’ from the given string.

 

Input. One single string containing no more than 1000 Latin letters and spaces.

 

Output. Print the string without lowercase Latin letters ‘a’, preserving the order of the other characters.

 

Sample input

Sample output

abrakadabra

brkdbr

 

 

SOLUTION

strings

 

Algorithm analysis

Initialize two pointers, both initially set to the beginning of the array: i = j = 0. Traverse the string using pointer i. For each character s[i] that is not equal to a, copy it to s[j] and move pointer j one position forward.

 

Algorithm implementation

Declare a character array.

 

char s[1001];

 

Read the input string.

 

fgets(s, sizeof(s), stdin);

 

Move the letters that are not a to the left.

 

int j = 0;

for (int i = 0; i < strlen(s); i++)

  if (s[i] != 'a') s[j++] = s[i];

 

At the end of the resulting string, place a null byte 0.

 

s[j] = 0;

 

Print the answer.

 

puts(s);

 

Algorithm implementation – Ñ++

Read the input string.

 

getline(cin, s);

 

Add characters that are not a to the resulting string res.

 

for (i = 0; i < s.length(); i++)

  if (s[i] != 'a') res.push_back(s[i]);

 

Print the answer.

 

cout << res;

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();

 

    String res = "";

    for(int i = 0; i < s.length(); i++)

      if (s.charAt(i) != 'a') res = res + s.charAt(i);

 

    System.out.printf(res);

    con.close();

  }

}

 

Java implementation – replace

 

import java.util.*;

 

public class Main

{

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    String s = con.nextLine();

    s = s.replace("a", "");

    System.out.printf(s);

    con.close();

  }

}

 

Python implementation

Read the input string.

 

s = input()

 

To remove all letters a from the string, use the replace() method. It creates a new string by replacing all occurrences of the specified substring with another substring.

replace(old, new)

·     old: The substring to be replaced.

·     new: The substring to replace it with.

 

print(s.replace('a', ''))